<div ng-controller="tianjiaC">
  <div class="bg-light lter b-b wrapper-md">
    <h1 class="m-n font-thin h3">添加员工</h1>
  </div>
  <div class="wrapper-md">
    <div class="panel panel-default">
      <div class="table-responsive">
        <form name="form" class="form-validation">
          <div class="panel panel-default">
            <div class="panel-heading">
              <span class="h4">添加员工</span>
            </div>
            <div class="panel-body">
              <div class="form-group">
                <label>用户名 <em class="text-muted">(2-15字符)</em></label>
                <input type="text" class="form-control" ng-model="userdata.Name" minlength="2" maxlength="15" required >
              </div>
              <div class="form-group">
                <label class="control-label">部门</label>
                <select required class="form-control" ng-options="y.Id as y.Name for y in arr" ng-model="userdata.DepartmentId"></select>
              </div>
                <div class="form-group">
                    <label>职务(非必填)</label>
                    <select class="form-control" ng-options=" x.Value as x.Name for x in OfficesData" ng-model="userdata.Offices"></select>
                </div>
              <div class="form-group">
                <label>身份证(非必填)</label>
                <input type="text" class="form-control" ng-model="userdata.IDCard" ng-pattern="/^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/" >
              </div>
              <div class="form-group">
                <label>登录名 <em class="text-muted">以字母开头的6-15位字母或数字(不能和用户名相同)</em></label>
                <input type="text" class="form-control" ng-model="userdata.LoginName" ng-pattern="/^[a-z]\w{5,14}$/" required>
              </div>
              <div class="form-group pull-in clearfix">
                <div class="col-sm-6">
                  <label>密码 <em class="text-muted">(请输入6-15位字母或数字)</em></label>
                  <input type="password" class="form-control" ng-pattern="/^[a-zA-Z0-9]{6,15}$/" name="password" ng-model="userdata.LoginPwd" required >
                </div>
                <div class="col-sm-6">
                  <label>电话</label>
                  <input type="text" class="form-control" ng-model="userdata.Phone" ng-pattern="/^[1][3-8]\d{9}$/" required >
                </div>
              </div>
              <div class="form-group">
                <label>生日(非必填)</label>
                <!--<input type="text" class="form-control" ng-model="userdata.Birthday" ng-pattern="/^(^\d{4}-\d{2}-\d{2})$/" placeholder="例如2000-01-01" value="{{userdata.Birthday}}">-->
                <input type="date" ng-model="userdata.Birthday" class="form-control">
              </div>
            </div>
            <footer class="panel-footer text-right bg-light lter">
              <button type="button" ng-click="submituser()" class="btn btn-success" ng-disabled="form.$invalid">提交</button>
            </footer>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<script>
    angular.module('app')
        //添加员工控制器
    .controller("tianjiaC", function ($scope, $state, $http) {
        $scope.userdata = {};
        $scope.userdata.Birthday = new Date();
        $http.get('../api/Department')
            .success(function (res) {
                $scope.departments = res.Data;
                $scope.traverseTree(res.Data);
//                $scope.traverseTree(res.Data , 0);
            });
        $scope.submituser = function () {
//            console.log($scope.userdata);
            $http.post('../api/Employee', $scope.userdata)
                .success(function (res) {
                    if (res.State <= 0) {
                        alert(res.Msg);
                    } else {
                        alert("添加成功");
                        $state.go("app.Employee_List");
                    }
                });
        };
        $scope.arr = [];
        $scope.traverseTree = function(node){

            for (var i = 0; i < node.length; i++) {
                var empty = '　└─';
                for (var j = 1; j < node[i].Item1; j++) {
                    empty = '　　'+ empty
                }
                if(i != 0){
                    node[i].Item2.Name = empty+node[i].Item2.Name;
                }else{
                    node[i].Item2.Name = '　'+node[i].Item2.Name;
                }
                $scope.arr.push(node[i].Item2);
            }
        };
        //递归算法
//        $scope.arr = [];
//        $scope.traverseTree = function(node,aa){
//            if (!node) {
//                return;
//            }
//            if (node && node.length > 0) {
//                var i = 0;
//                aa = aa +1;
//                for (i = 0; i < node.length; i++) {
//                    var item = node[i];
//                    var empty = '';
//                    for (index = 1; index < aa; index++) {
//                        if(index == (aa-1)){
//                            empty  = empty + '　└─'
//                        }else{
//                            empty  = empty + '　'
//                        }
//
//                    }
//                    item.Name = empty+item.Name;
////                    $scope.obj = {Name:empty+item.Name , Id: item.Id};
//                    $scope.arr.push(item);
////                    console.log($scope.arr);
//                    this.traverseTree(node[i].Children,aa);
//                }
//            }
//        };
    })

</script>